<template  >
  <v-form v-model="valid" ref="mytzForm">
    <v-container grid-list-md>
      <v-layout wrap>
        <v-flex xs12 sm6>
          <v-text-field v-model="purchase.lsh" label="流水号：" disabled></v-text-field>
        </v-flex>
        <v-flex xs12 sm6>
          <v-select
            :disabled="isEdit"
            v-model="purchase.storeCode"
            item-text="storeName"
            item-value="storeCode"
            :items="stores"
            label="店铺"
            required
          />
        </v-flex>
        <v-flex xs12 sm6>
          <v-text-field :rules="nameRules" v-model="purchase.hh" label="货号" @change="onchange" />
        </v-flex>
        <v-flex xs12 sm6>
          <v-text-field v-model="purchase.pinm" label="品名" disabled />
        </v-flex>
        <v-flex xs12 sm6>
          <v-text-field v-model="purchase.sl" label="数量" />
        </v-flex>
        <v-flex xs12 sm6>
          <v-text-field v-model="purchase.truename" label="备注" />
        </v-flex>

        <v-flex v-if="isEdit" xs12 sm6>
          <v-text-field v-model="purchase.truename" label="录入人：" hint disabled></v-text-field>
        </v-flex>
        <v-flex v-if="isEdit" xs12 sm6>
          <v-text-field
            v-model="purchase.lrsj"
            label="录入时间"
            :disabled="isEdit"
            hint
            persistent-hint
          ></v-text-field>
        </v-flex>
        <v-flex xs12 sm6>
          <v-select
            :disabled="isEdit"
            v-model="purchase.oldLx"
            item-text="text"
            item-value="value"
            :items="items"
            label="原类型"
            required
          />
        </v-flex>
        <v-flex xs12 sm6>
          <v-select
            :disabled="isEdit"
            v-model="purchase.newLx"
            item-text="text"
            item-value="value"
            :items="items"
            label="新类型"
            required
          />
        </v-flex>
      </v-layout>
    </v-container>

    <v-layout class="my-4" row>
      <v-spacer />
      <v-btn v-if="!purchase.lsh" @click="submit" color="primary">保存</v-btn>
      <v-btn v-if="!purchase.lsh" @click="clear">重置</v-btn>
      <v-btn v-if="purchase.lsh && purchase.status === 0" @click="commit">提交</v-btn>
      <v-btn v-if="purchase.lsh &&  purchase.status === 0" @click="del">作废</v-btn>

      <v-btn v-if="purchase.status===1" @click="shenhe(1)">审核</v-btn>
      <v-btn v-if="purchase.status===2" @click="shenhe(2)">审核</v-btn>
    </v-layout>
  </v-form>
</template>
<script>
export default {
  name: "purchase-form",
  props: {
    oldpurchase: {
      type: Object
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      valid: false, // 表单校验结果标记
      purchase: {
        lsh: "", //
        lrid: "", //
        status: 0, //
        stores: [],
        storeCode: "",
        oldLx: "",
        newLx: "",
        hh: "",
        sl: "",
        pinm: ""
      },
      stores: [],
      nameRules: [
        v => !!v || "货号不能为空",
      ],
      checkbox: false,
      items: [
        { text: "正品", value: 0 },
        { text: "样机", value: 1 },
        { text: "赠品", value: 2 },
        { text: "残次", value: 3 }
      ]
    };
  },
  methods: {
    getDataFromServer() {
      this.$http.get("/item/user/store").then(res => {
        this.stores = res.data;
      });
    },
    shenhe(a) {
      const { ...params } = this.purchase;
      params.username = window.localStorage.getItem("username");
      params.lrid = window.localStorage.getItem("id");
      params.truename = window.localStorage.getItem("truename");

      this.$http({
        method: "post",
        url: a === 1 ? "/item/lxtz/shenhe1" : "/item/lxtz/shenhe2",
        data: JSON.stringify(params),
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        }
      })
        .then(() => {
          // 关闭窗口
          this.$emit("close");
          this.$message.success("保存成功！");
        })
        .catch(() => {
          this.$message.error("保存失败！");
        });
    },
    onchange(hh) {
      hh = Number(hh);
      this.$http({
        method: "get",
        url: "item/item/queryhh?hh=" + hh,
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        }
      })
        .then(resp => {
          // 关闭窗口
          // this.editedItem = resp.data;
          this.purchase.pinm = resp.data.pinm;
        })
        .catch(() => {
          this.editedItem = {};
        });
    },
    clear() {
      // 重置表单
      this.$refs.mytzForm.reset();
    }
  },
  watch: {
    oldpurchase: {
      // 监控oldBrand的变化

      handler(val) {
        this.getDataFromServer();

        if (val) {
          // 注意不要直接复制，否则这边的修改会影响到父组件的数据，copy属性即可
          this.purchase = Object.deepCopy(val);
        } else {
          // 为空，初始化brand
          this.purchase = {
            name: "",
            code: "",
            id: "",
            status: 0,
            stores: []
          };
        }
      },
      deep: true
    }
  },
  components: {}
};
</script>

<style scoped>
</style>
